package Operations.Export;

import Data.DbData;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

/**
 * @author Martin
 * @version 1.0
 * @created 14-III-2012 13:12:29
 * Musi byt zobrazitelne v OpenOfficech Calc, staci dva neformatovane sloupce (datum, flow)
 * Musime si mit moznost vybrat umisteni a nazev vytvareneho souboru!!
 */
public class Export extends AExport{
    
    String format_key; 
    
	public Export(){
            this.format_key ="dd.MM.yyyy HH:mm";
	}
        
        public Export(String key){
            this.format_key = key;
        }
        
        public void setFormatKey(String key){
            this.format_key = key;
        }
        
        public boolean doExport(ArrayList<DbData> dbData,String file){
            return this.toCSV(dbData, ";", file);
        }
        
	/**
	 * 
	 * @param dbData
	 */
	private boolean toCSV(ArrayList<DbData> dbData, String separator, String file){
            //mozna by se vyplatil jeste buffer
            //jinak to bude pomerne casove narocna operace, hodilo by se pouzit jine vlakno a ProgresMonitor na ukazovani prubehu!!
            SimpleDateFormat format = new SimpleDateFormat(this.format_key);
            PrintStream ps = null;
            try{
                ps = new PrintStream(file);
                
                for (int i=0;i<dbData.size();i++){
                    //System.out.println(dbData.get(i).getDate().toString());
                    ps.println(format.format(dbData.get(i).getDate()) + separator + String.valueOf(dbData.get(i).getFlow()).replace(".", ",") );
                }
                
                ps.close();
            }catch(FileNotFoundException es){
                return false;
            }
            
		return true;
	}

}